﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.Complete.RCVRpt;
using UFIDA.U9.Cust.YYD.PlugBE.Dtos;
using UFIDA.U9.Cust.YYD.PlugBE.Public;
using UFIDA.U9.PM.PMIssue;
using UFSoft.UBF.Business;

namespace UFIDA.U9.Cust.YYD.PlugBE.PMIssueDocP
{
    public class BeforeInserting : UFSoft.UBF.Eventing.IEventSubscriber
    {
        public void Notify(params object[] args)
        {
            #region 从事件参数中取得当前业务实体													 
            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent))
                return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;
            if (key == null)
                return;
            var holder = key.GetEntity() as UFIDA.U9.PM.PMIssue.PMIssueDoc;
            if (holder == null)
                return;
            #endregion

            if (!string.IsNullOrEmpty(holder.DescFlexField.PrivateDescSeg1))
            {
                var ckdoc = PMIssueDoc.Finder.Find($"DescFlexField.PrivateDescSeg1 = '{holder.DescFlexField.PrivateDescSeg1}' and ID != {holder.ID}");
                if (ckdoc != null)
                {
                    throw new Exception($"WMS重复单号{ckdoc.DocNo}");
                }
            }
        }
    }
}
